<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <script type="text/javascript">
      var newTemplateWin;
      var editTemplateWin;
      var templateId;
      var templateDS;

      function standardWindow(id){
        standardWin = new Ext.Window({                                       //window和上面的fuction不能重名，否则会出问题
          layout: 'fit',
          width: 890,
          height: 425,
          closeAction: 'destroy',
          autoLoad: {
            url: "/standardmaintenance/standardMaintenanceList",
            params:{id:id},
            scripts: true
          },
          plain: true,
          modal: true,
          constrain:true,
          autoScroll: true,
          draggable: false                                                      //不能拖动
        });
        standardWin.on("destroy",function(){
          templateDS.reload();
        });
        standardWin.setTitle("模板管理");
        standardWin.resizable = false;
        standardWin.show();
      }
      Ext.onReady(function(){
        //检修模板管理---------------------------------------------------------
        //生成有关检修模板的Grid
        templateDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize
          },
          proxy: new Ext.data.HttpProxy({
            url: '/template/getTemplates'
          }),
          reader:new Ext.data.JsonReader({
            totalProperty: 'total',
            root:'template'
          },[
            {name:'id'},
            {name:'name'},
            {name:'is_locked'},
            {name:'sub_type',mapping:'sub_type',
              convert:function(value){
                if(value==0){
                  return '静态作业'
                }else if(value==1){
                  return '动态作业'
                }else{
                  return value
                }
              }
            }
          ]
        )
        });

        var templateSM = new Ext.grid.CheckboxSelectionModel();
        var templateCM = new Ext.grid.ColumnModel([
          new Ext.grid.RowNumberer({width: 30}),
          templateSM,
          {
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderIsLucked,
            width: 70
          },
          {header:'模板名称',sortable:true,dataIndex:'name',width:230},
          {header: '维护详情',dataIndex: 'manufacturer',renderer: standardRender,width:80},
          {header: '工作类型',dataIndex: 'sub_type',width:80}
        ]);

        function standardRender(value, cellmeta, record, rowIndex, columnIndex, store){
          var id = store.getAt(rowIndex).get('id');
          return "<center><a style=cursor:pointer;color:blue onclick=standardWindow("+id+")>进入</a></center>"
        }
        var templateGrid =  new Ext.grid.GridPanel({
          ds: templateDS,
          cm: templateCM,
          sm: templateSM,
          width: screenWidth-240,
          height: screenHeight-310,
          iconCls: 'icon-grid',
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize,
            store: templateDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          }),
          tbar: ['-',{
              text: '添加养护任务模板',
              width:50,
              iconCls: 'addItem',
              handler : function(){
                newTemplateWin = new Ext.Window({
                  layout: 'fit',
                  width:267,
                  height:189,
                  closeAction: 'destroy',
                  plain: true,
                  modal: true,
                  constrain:true,
                  //modal: true,
                  title: '添加养护任务模板',
                  autoLoad: {
                    url: "/template/newTemplate",
                    scripts: true
                  }
                });
                newTemplateWin.on("destroy",function(){
                  templateDS.reload();
                });
                newTemplateWin.resizable = false;
                newTemplateWin.show();
              }
            },{
              text: '编辑养护任务模板',
              width:50,
              iconCls: 'editItem',
              handler : function(){
                var records = templateGrid.getSelectionModel().getSelections();
                if(records.length==0){
                  Ext.MessageBox.alert('提示信息', '请先选中一条记录后，再编辑。');
                }else{
                  //把表单添加到窗口中
                  templateId = records[0].get('id');
                  editTemplateWin = new Ext.Window({
                    title: '编辑养护任务模板',
                    layout:'fit',
                    width:267,
                    height:189,
                    closeAction:'destroy',
                    constrain:true,
                    plain: true,
                    modal: true,
                    autoLoad: {
                      url: "/template/editTemplate?id=" + templateId,
                      scripts: true
                    }
                  });
                  editTemplateWin.on("destroy",function(){
                    templateDS.reload();
                  });
                  editTemplateWin.resizable = false;
                  editTemplateWin.show();
                }

              }
            },
            {
              text:'锁定或锁定养护任务模板',
              width:50,
              iconCls: 'remove',
              handler:function(){
                if(templateGrid.getSelectionModel().hasSelection()){
                  var rs=templateGrid.getSelectionModel().getSelections();
                  var ids=[];
                  var templateName='';
                  for(var i=0;i<rs.length;i++){
                    var data=rs[i].data;
                    ids.push(data.id);
                    templateName+=data.name+'<br>';
                  }
                  var keys=Ext.util.JSON.encode(ids);

                  Ext.Ajax.request({
                    url:'/template/doLock',
                    method:'post',
                    waitMsg: '正在锁定或锁定养护任务模板...',
                    success:function(response,opts){
                      var data = Ext.util.JSON.decode(response.responseText);
                      if(data.success&&data.info=='success') {
                        templateDS.reload();
                      } else {
                        Ext.Msg.alert('提示信息', data.info);
                      }
                    },
                    params: {"id":keys}
                  });
                }else{
                  Ext.Msg.alert('提示信息','请选择要锁定或锁定的养护任务模板');
                }
              }
            }]
        });
        templateGrid.render('templateList');
        templateDS.load();
      })
    </script>
  </head>
  <body>
    <div id="templateList"></div>
  </body>
</html>
